﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Base;
using ERP.BusinessEntities;
using System.Windows.Forms;
using System.IO;
using Core;
using System.Data;

namespace MainApp.Modules.Product
{
    public class ProductModule: BaseModule
    {
        public ProductModule()
        {
            this.Name = "Product";
            entity = new ProductEntity();
            Init();
        }


        #region Event
        public void ShowUOMConfig()
        {
            if (dicScreens.ContainsKey("StockUOMScreen"))
                dicScreens["StockUOMScreen"].ShowDialog(scrStartScreen);
        }
        #endregion

        public override void Import()
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Multiselect = false;
            openFileDlg.ShowDialog();

            String strFileName = openFileDlg.FileName;

            if (File.Exists(strFileName) == false) return;

            ExcelClient exClient = new ExcelClient(strFileName);
            exClient.OpenConnection();
            String[] lstSheets = exClient.GetSheetName();
            DataTable dt = exClient.GetAllDataFromSheet(lstSheets[0]);
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    ICProductsInfo Product = new ICProductsInfo();
                    Product.No = Convert.ToString(dr[0]);
                    Product.Name = Convert.ToString(dr[1]);
                    if(dr[3] != null && dr[3] != DBNull.Value)
                        Product.SaleUnitPrice = Convert.ToDouble(dr[3]);
                    if (dr[4] != null && dr[4] != DBNull.Value)
                        Product.PurchaseUnitPrice = Convert.ToDouble(dr[4]);
                    Product.Dsc = Product.Name;
                    Product.FK_ICStockUOMs_ID = 1;
                    Product.FK_ICStocks_ID = 1;
                    Product.FK_ICProductCats_ID = 7;
                    Product.FK_TxTaxCodes_OutVATID = 1;
                    Product.FK_TxTaxCodes_InVATID = 6;
                    Product.MinQty = 6;
                    Product.isStockItem = true;
                    Product.CreatedDate = DateTime.Now;
                    Product.UpdatedDate = DateTime.Now;
                    AppContext.ProductCtrl.CreateObject(Product);
                }
                catch (Exception)
                {
                    
                }
                
            }

        }

    }
}
